﻿Imports Framework.Data

Public Class SharedItems
    Shared Sub New()
        Framework.Security.KeyMaster.RegisterDissolver(Of Entities.SharedItem)(AddressOf SecurityGroupDissolver)
    End Sub
    Private Shared Function SecurityGroupDissolver(ByVal item As Entities.SharedItem) As Generic.HashSet(Of Integer)
        Dim clsG As Entities.Share = Shares.GetById(item.ShareId)
        If clsG Is Nothing Then
            Return Nothing
        Else
            Return clsG.SecurityGroupIds
        End If
    End Function

#Region " Events "
    Public Shared Event Deleted(ByVal item As Entities.SharedItem)
    Public Shared Event DeletedGUI(ByVal item As Entities.SharedItem)
    Friend Shared Sub OnDeleted(ByVal item As Entities.SharedItem)
        RaiseEvent Deleted(item)
        Framework.Threading.Invoke(AddressOf OnDeleted_Callback, item)
    End Sub
    Private Shared Sub OnDeleted_Callback(ByVal item As Entities.SharedItem)
        RaiseEvent DeletedGUI(item)
    End Sub

    Public Shared Event Updated(ByVal item As Entities.SharedItem)
    Public Shared Event UpdatedGUI(ByVal item As Entities.SharedItem)
    Friend Shared Sub OnUpdated(ByVal item As Entities.SharedItem)
        RaiseEvent Updated(item)
        Framework.Threading.Invoke(AddressOf OnUpdated_Callback, item)
    End Sub
    Private Shared Sub OnUpdated_Callback(ByVal item As Entities.SharedItem)
        RaiseEvent UpdatedGUI(item)
    End Sub
#End Region

    Public Shared Function GetById(ByVal id As Integer) As Entities.SharedItem
        Return DataStore.SharedItemsEx.GetById(id, New TransactionScope(TransactionScope.TransactionMode.NullTransaction))
    End Function
    Public Shared Function GetById(ByVal id As Integer, ByVal transactionScope As TransactionScope) As Entities.SharedItem
        Return DataStore.SharedItemsEx.GetById(id, transactionScope)
    End Function
    Public Shared Function GetAllByShareIdAndRelativeDirectory(ByVal shareId As Integer, ByVal relativeDirectory As String) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByShareIdAndRelativeDirectory(shareId, relativeDirectory)
    End Function
    Public Shared Function GetAllByShareIdAndDirectoryRecursive(ByVal shareId As Integer, ByVal directory As String) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByShareIdAndDirectoryRecursive(shareId, directory)
    End Function
    Public Shared Function GetAllSubdirectoriesByShareIdAndRelativeDirectory_Dis(ByVal shareId As Integer, ByVal relativeDirectory As String) As Generic.List(Of String)
        Dim aTmp As Generic.List(Of String) = DataStore.SharedItemsEx.GetAllSubdirectoriesByShareIdAndRelativeDirectory(shareId, relativeDirectory)

        Dim aRet As New Generic.SortedList(Of String, String)
        For Each strTmp As String In aTmp
            strTmp = strTmp.Substring(relativeDirectory.Length)
            While strTmp.StartsWith(IO.Path.DirectorySeparatorChar) Or strTmp.StartsWith(IO.Path.AltDirectorySeparatorChar)
                strTmp = strTmp.Substring(1)
            End While
            Dim aParts() As String = strTmp.Split(IO.Path.DirectorySeparatorChar)
            If Not aRet.ContainsKey(aParts(0).ToLower) Then aRet.Add(aParts(0).ToLower, aParts(0))
        Next
        Return aRet.Values.ToList
    End Function

    Public Shared Function GetAllSubdirectoriesByShareIdAndRelativeDirectoryAsInfos(ByVal shareId As Integer, ByVal relativeDirectory As String) As Generic.List(Of Entities.Infos.DirectoryInfo)
        Dim aTmp As Generic.List(Of Entities.Infos.DirectoryInfo) = DataStore.SharedItemsEx.GetAllSubdirectoriesByShareIdAndRelativeDirectoryAsInfos(shareId, relativeDirectory)

        Dim aRet As New Generic.SortedList(Of String, Entities.Infos.DirectoryInfo)
        For Each clsDir As Entities.Infos.DirectoryInfo In aTmp
            Dim strTmp As String
            strTmp = clsDir.Name.Substring(relativeDirectory.Length)
            While strTmp.StartsWith(IO.Path.DirectorySeparatorChar) Or strTmp.StartsWith(IO.Path.AltDirectorySeparatorChar)
                strTmp = strTmp.Substring(1)
            End While
            Dim aParts() As String = strTmp.Split(IO.Path.DirectorySeparatorChar)
            Dim strKey As String = aParts(0).ToLower
            If Not aRet.ContainsKey(strKey) Then
                clsDir.Name = aParts(0)
                aRet.Add(strKey, clsDir)
            Else
                Dim clsDir2 As Entities.Infos.DirectoryInfo = aRet(strKey)
                clsDir2.TotalItems += clsDir.TotalItems
                clsDir2.TotalSize += clsDir.TotalSize
            End If
        Next
        Return aRet.Values.ToList
    End Function

    Public Shared Function GetAllByItemTypeAndMissingGenres(ByVal itemType As Entities.ItemType) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByItemTypeAndMissingGenres(itemType)
    End Function
    Public Shared Function GetAllByItemTypeAndMissingFeatureNeedingAction(ByVal itemType As Entities.ItemType) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByItemTypeAndMissingFeatureNeedingAction(itemType)
    End Function
    Public Shared Function GetAllByIds(ByVal ids As Generic.List(Of Integer)) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByIds(ids)
    End Function

    Public Shared Function GetAllByShareId(ByVal shareId As Integer) As Generic.List(Of Entities.SharedItem)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Return DataStore.SharedItemsEx.GetAllByShareId(shareId, t)
        End Using
    End Function
    Public Shared Function GetAllByShareIdAndAddedAfter(ByVal shareId As Integer, ByVal addedAfter As Date) As Generic.List(Of Entities.SharedItem)
        Return DataStore.SharedItemsEx.GetAllByShareIdAndAddedAfter(shareId, addedAfter)
    End Function

    Public Shared Sub Save(ByVal item As Entities.SharedItem, ByVal transactionScope As TransactionScope)
        DataStore.SharedItemsEx.Save(item, transactionScope)
        transactionScope.RegisterOnCompleteCallback(AddressOf OnUpdatedTransactionComplete, "SharedItemUpdated", item.Id, item)
    End Sub

    Public Shared Sub DeleteAllByShareId(ByVal shareId As Integer, ByVal transactionScope As TransactionScope)
        Dim aItems As Generic.List(Of Entities.SharedItem)
        aItems = DataStore.SharedItemsEx.GetAllByShareId(shareId, transactionScope)
        For Each clsItem As Entities.SharedItem In aItems
            Delete(clsItem, transactionScope)
        Next
    End Sub
    Public Shared Sub Delete(ByVal item As Entities.SharedItem)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Delete(item, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Delete(ByVal item As Entities.SharedItem, ByVal transactionScope As TransactionScope)
        'Dim clsItem As Entities.SharedItem = GetById(id, transactionScope)
        SharedItemsFiles.DeleteBySharedItemId(item.Id, transactionScope)
        MediaInfo.DeleteBySharedItemId(item.Id, transactionScope)
        DataStore.SharedItemsEx.Delete(item.Id, transactionScope)
        transactionScope.RegisterOnCompleteCallback(AddressOf OnDeletedTransactionComplete, "SharedItemDeleted", item.Id, item)
        'OnDeleted(item)
    End Sub
    Private Shared Sub OnUpdatedTransactionComplete(ByVal arg1 As String, ByVal arg2 As Integer, ByVal arg3 As Object)
        OnUpdated(CType(arg3, Entities.SharedItem))
    End Sub
    Private Shared Sub OnDeletedTransactionComplete(ByVal arg1 As String, ByVal arg2 As Integer, ByVal arg3 As Object)
        OnDeleted(CType(arg3, Entities.SharedItem))
    End Sub

End Class

